/*
 * Copyright (c) 2009, 2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ALKES_TVECTOR2_H_INCLUDED_
#define ALKES_TVECTOR2_H_INCLUDED_

#include "alkes/internal.h"
#include "alkes/math/TFixed.h"

namespace alkes {

template<typename T>
struct TVector2 {
    typedef T   value_type;

    T   x;
    T   y;

    const static TVector2<T> ZERO;
    const static TVector2<T> HALF;
    const static TVector2<T> ONE;

    bool operator==(const TVector2& rh) const;
    bool operator!=(const TVector2& rh) const
    {
        return !(*this == rh);
    }
};

template<typename T>
const TVector2<T> TVector2<T>::ZERO = {
    (T)0,
    (T)0
};

template<typename T>
const TVector2<T> TVector2<T>::ONE = {
    (T)1,
    (T)1
};

template<typename T>
const TVector2<T> TVector2<T>::HALF = {
    (T)0.5f,
    (T)0.5f
};

template<typename T>
bool TVector2<T>::operator==(const TVector2& rh) const
{
    return x == rh.x && y == rh.y;
}

}

#define TYPE    alkes::TVector2<T>

#define DEFINE_OP(op)                                       \
    template<typename T>                                    \
    const TYPE operator op(const TYPE& lh, const TYPE& rh)  \
    {                                                       \
        TYPE v = {lh.x op rh.x, lh.y op rh.y};              \
        return v;                                           \
    }

DEFINE_OP(+)
DEFINE_OP(-)
DEFINE_OP(*)
DEFINE_OP(/)

#undef DEFINE_OP
#define DEFINE_OP(op)                                       \
    template<typename T, typename U>                        \
    const TYPE operator op(const TYPE& lh, const U rh)      \
    {                                                       \
        TYPE v = {T(lh.x op rh), T(lh.y op rh)};            \
        return v;                                           \
    }

DEFINE_OP(+)
DEFINE_OP(-)
DEFINE_OP(*)
DEFINE_OP(/)

#undef DEFINE_OP
#define DEFINE_OP(op)                                       \
    template<typename T, typename U>                        \
    const TYPE operator op(const U lh, const TYPE& rh)      \
    {                                                       \
        TYPE v = {T(lh op rh.x), T(lh op rh.y)};            \
        return v;                                           \
    }

DEFINE_OP(+)
DEFINE_OP(-)
DEFINE_OP(*)
DEFINE_OP(/)

#undef DEFINE_OP
#define DEFINE_OP(op)                                       \
    template<typename T>                                    \
    const TYPE& operator op(TYPE& lh, const TYPE& rh)       \
    {                                                       \
        lh.x op rh.x; lh.y op rh.y;                         \
        return lh;                                          \
    }

DEFINE_OP(+=)
DEFINE_OP(-=)
DEFINE_OP(*=)
DEFINE_OP(/=)

#undef DEFINE_OP
#define DEFINE_OP(op)                                       \
    template<typename T, typename U>                        \
    const TYPE& operator op(TYPE& lh, const U rh)           \
    {                                                       \
        lh.x op rh; lh.y op rh;                             \
        return lh;                                          \
    }

DEFINE_OP(+=)
DEFINE_OP(-=)
DEFINE_OP(*=)
DEFINE_OP(/=)

#undef DEFINE_OP
#undef TYPE

#endif
